package com.nuvo.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.validator.constraints.Email;

@Entity
@Table(name = "USUARIO", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"LOGIN"})})
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Usuario.buscarTodos", query = "SELECT u FROM Usuario u"),

    @NamedQuery(name = "Usuario.buscarLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login AND u.clave = :clave"),
    @NamedQuery(name = "Usuario.validaUser", query = "SELECT u FROM Usuario u WHERE u.login = :login")})

public class Usuario implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Size(min = 1, max = 100, message = "debe ingresar un nombre")
    @Column(name = "NOMBRE", nullable = false)
    private String nombre;

    @Column(name = "CREATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    @Column(name = "UPDATED")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;

    @Size(min = 3, max = 100, message = "Debe ingresar el Login")
    @Column(name = "LOGIN")
    private String login;

    @Size(min = 3, max = 100, message = "Debe ingresar la Clave")
    @Column(name = "CLAVE")
    private String clave;

    @Size(min = 1, max = 100, message = "formato no valido o datos imcompletos")
    @Column(name = "EMAIL")
    @Email
    private String email;

    @NotNull(message = "debe seleccionar un tipo de usuario")
    @JoinColumn(name = "TIPOUSUARIO_ID", referencedColumnName = "ID")
    @ManyToOne
    private Tipousuario tipousuarioId;

    public Usuario() {
    }

    public Usuario(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {

        this.login = login;

    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public String getClave() {
        return clave;
    }

    public void setClave(String clave) {
        this.clave = clave;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Tipousuario getTipousuarioId() {
        return tipousuarioId;
    }

    public void setTipousuarioId(Tipousuario tipousuarioId) {
        this.tipousuarioId = tipousuarioId;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 61 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Usuario other = (Usuario) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return nombre + " (" + id + ")";
    }

}
